/*


 */

package jas.rpc.client;

import jas.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 *
 */
public class Base64ClientSerializer implements ObjectClientSerializer {
    

    public Object deserializeObject(String s, RpcClient client, StubFactory stubFactory) throws ClassNotFoundException {
        try {
            byte[] bytes = Base64.decode(s, Base64.DONT_BREAK_LINES);
            if (bytes == null)
                throw new IllegalArgumentException("Not base64: " + s);
            
            ObjectInputStream st = new RpcClientObjectInputStream(new ByteArrayInputStream(bytes), client, stubFactory);
            return st.readObject();
            
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
            return null;
        }
    }
    
    public String serializeObject(Object o) {
        try {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ObjectOutputStream stream = new RpcClientObjectOutputStream(buffer);
            stream.writeObject(o);
            String s = Base64.encodeBytes(buffer.toByteArray(), Base64.DONT_BREAK_LINES);
            return s;
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
            return null;
        }
    }
    
}
